package com.kuaiyin.controller.request;


import com.kuaiyin.dto.TestPlanDTO;
import com.kuaiyin.entity.TestPlan;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.ITestPlanService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.TestPlanVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 测试计划表 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-30
 */
@Slf4j
@Tag(name = "测试计划管理")
@RestController
@RequestMapping("/plan")
public class TestPlanController {

    @Autowired
    private ITestPlanService testPlanService;


    @Operation(summary = "新增计划")
    @PostMapping()
    public Result<String> saveTestPlan(@Valid @RequestBody TestPlanDTO testPlanDTO) {
        testPlanService.saveTestPlan(testPlanDTO);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新计划")
    @PutMapping("/{id}")
    public Result<String> updateTestPlan(@PathVariable Long id, @Valid @RequestBody TestPlanDTO testPlanDTO) {
        testPlanService.updateTestPlan(id, testPlanDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除计划")
    @DeleteMapping("/{id}")
    public Result<String> deleteTestPlan(@PathVariable Long id) {
        testPlanService.removeById(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "更新测试计划状态")
    @PutMapping("/{id}/status/{status}")
    public Result<String> updateTestPlanStatus(@PathVariable Long id, @PathVariable Integer status) {
        testPlanService.updateTestPlanStatus(id, status);
        return Result.success("更新成功");
    }

    @Operation(summary = "测试计划列表")
    @GetMapping("/all")
    public Result<List<TestPlanVO>> getTestPlanList() {
        List<TestPlanVO> testPlanList = testPlanService.getTestPlanList();
        return Result.success(testPlanList);
    }

    @Operation(summary = "测试计划详情")
    @GetMapping("/{id}")
    public Result<TestPlanVO> getTestPlan(@PathVariable Long id) {
        TestPlanVO testPlanVO = testPlanService.getById(id);
        return Result.success(testPlanVO);
    }

    @Operation(summary = "分页查询测试计划列表")
    @GetMapping("/search")
    public Result<PageResult<TestPlanVO>> getTestPlan(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<TestPlanVO> testPlanPage = testPlanService.getTestPlanPage(page, size, wrapper -> {
            if (StringUtils.hasText(name)) {
                wrapper.like(TestPlan::getName, name);
            }
            wrapper.orderByDesc(TestPlan::getId);
        });
        return Result.success(testPlanPage);
    }
}

